﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kiiro.Common.Entities;

namespace Kiiro.Common.Utility
{
    public static class FormattingHelpers
    {
        private static double MinutesPerHour = 60.0;
        private static double DaysPerWeek = 5.0;
        private static double DaysPerMonth = 20.0;

        public static string ListOfStringsToFieldText ( List<string> strings )
        {
            StringBuilder fieldText = new StringBuilder ( );

            if ( strings != null )
            {
                bool first = true;
                foreach ( string str in strings )
                {
                    if ( first )
                        fieldText.Append ( ";#" );

                    fieldText.Append ( string.Format ( "{0};#", str ) );

                    first = false;
                }
            }

            return fieldText.ToString ( );
        }

        public static string ListOfKiiroGroupsToFieldText ( List<KiiroGroup> groups )
        {
            StringBuilder fieldText = new StringBuilder ( );

            if ( groups != null )
            {
                bool first = true;
                foreach ( KiiroGroup group in groups )
                {
                    if ( first )
                        fieldText.Append ( ";#" );

                    fieldText.Append ( string.Format ( "{0};#", group.ToString ( ) ) );

                    first = false;
                }
            }

            return fieldText.ToString ( );
        }

        public static string ListOfStringsToDoubleFieldText(List<string> strings)
        {
            StringBuilder fieldText = new StringBuilder();

            if (strings != null)
            {
                bool first = true;
                foreach (string str in strings)
                {
                    if (first)
                        fieldText.Append(";#");

                    fieldText.Append(string.Format("{0};#", str));

                    fieldText.Append(string.Format("{0};#", str));

                    first = false;
                }
            }

            return fieldText.ToString();
        }

        public static string ListOfIntsToFieldText(List<int> ints)
        {
            List<string> strings = new List<string>();

            foreach (int integer in ints)
            {
                strings.Add(integer.ToString());
            }

            return ListOfStringsToFieldText(strings);
        }

        public static string ListOfIntsToDoubleFieldText(List<int> ints)
        {
            List<string> strings = new List<string>();

            foreach (int integer in ints)
            {
                strings.Add(integer.ToString());
            }

            return ListOfStringsToDoubleFieldText(strings);
        }


        public static List<string> FieldTextToListOfStrings ( string fieldText )
        {
            return fieldText.Split ( new char [ ] { '#', ';' }, StringSplitOptions.RemoveEmptyEntries ).ToList ( );
        }

        public static string GetErrorMessageText ( List<ValidationMessage> messages )
        {
            StringBuilder errorMessage = new StringBuilder ( );

            if ( messages != null )
            {
                bool first = true;
                foreach ( ValidationMessage message in messages )
                {
                    if ( !first )
                        errorMessage.Append ( "<br/>" );

                    errorMessage.Append ( message.Message );

                    first = false;
                }
            }

            return errorMessage.ToString ( );
        }

        public static string BooleanToDisplayString ( bool value )
        {
            return value ? "Yes" : "No";
        }

        public static string ListOfStringsToDisplayString ( List<string> values )
        {
            StringBuilder displayString = new StringBuilder ( );

            if ( values != null )
            {
                bool first = true;
                foreach ( string value in values )
                {
                    if ( !first )
                        displayString.Append ( ", " );

                    displayString.Append ( value );

                    first = false;
                }
            }

            return displayString.ToString ( );
        }

        public static string DateTimeToStringOrDashes(DateTime value)
        {
            string valueString = "";
            if (value != null && value != DateTime.MinValue)
                valueString = value.ToString("MMM dd, yyyy");
            else
                valueString = "- - -";
            return valueString;
        }

        public static string DateTimeToDatePickerString ( DateTime value )
        {
            return value.ToString ( "MM/dd/yyyy" );
        }
        
        public static string DateTimeToDisplayString ( DateTime value )
        {
            string date = value.ToString ( "dddd, MMMM d @ h:mm tt" );
            return date;
        }

        public static string DateTimeToDisplayDateNoTimeString ( DateTime value )
        {
            string date = string.Empty;
            DateTime now = DateTime.Now;

            if ( DateTime.Compare ( now.Date, value.Date ) > 0 )
            {
                TimeSpan ts = now.Subtract ( value );
                if ( ts.Days == 1 )
                {
                    date = "Yesterday" ;
                }
                else
                {
                    date = value.ToString ( "MMM dd, yyyy" ); 
                }
            }
            else
            {
                date = "Today";
            }
            return date;
        }

        public static string DateTimeToDisplayDateStringNoLine(DateTime value)
        {
            string date = string.Empty;
            DateTime now = DateTime.Now;
    
            if ( DateTime.Compare(now.Date, value.Date) > 0 )
            {
                TimeSpan ts = now.Date.Subtract ( value.Date );
                if ( ts.Days == 1 )
                {
                    date = "Yesterday " + value.ToString("@ h:mm tt");
                }
                else
                {
                    date = value.ToString ( "MMM d, yyyy @ h:mm tt" ); 
                }
            }
            else
            {
                date = "Today " + value.ToString("@ h:mm tt");
            }
            return date;
        }

        public static string DateTimeToDisplayDateString ( DateTime value )
        {
            string date = string.Empty;
            DateTime now = DateTime.Now;
    
            if ( DateTime.Compare(now.Date, value.Date) > 0 )
            {
                TimeSpan ts = now.Date.Subtract ( value.Date );
                if ( ts.Days == 1 )
                {
                    date = "Yesterday " + value.ToString("<br/>@ h:mm tt");
                }
                else
                {
                    date = value.ToString ( "MMM d, yyyy <br/>@ h:mm tt" ); 
                }
            }
            else
            {
                date = "Today " + value.ToString("<br/>@ h:mm tt");
            }
            return date;
        }

        public static string DateTimeToTimeAgoString(DateTime value)
        {
            string date = string.Empty;
            DateTime now = DateTime.Now;

            if (DateTime.Compare(now, value) >= 0)
            {
                TimeSpan ts = now.Subtract(value);
                TimeSpan ds = now.Date.Subtract(value.Date);
                if (ts.Days > 0)
                    date = ds.Days > 1 ? string.Format("{0} {1} Ago", ds.Days, "Days") : "Yesterday";
                else if (ts.Hours > 0)
                    date = string.Format("{0} {1} Ago", ts.Hours, ts.Hours > 1 ? "Hours" : "Hour");
                else
                    date = string.Format("{0} {1} Ago", ts.Minutes > 0 ? ts.Minutes.ToString() : "Less Than 1", ts.Minutes > 1 ? "Minutes" : "Minute");                
            }

            return date;
        }

        public static string LongBytesToDisplaySize(long value)
        {
            if (value > 1048576)
                return string.Format("{0} {1}", (Convert.ToDouble(value) / 1048576).ToString("#0.0"), "mb");
            else if (value > 1024)
                return string.Format("{0} {1}", value / 1024, "kb");
            else
                return string.Format("{0} {1}", value, "b");
        }


        public static float ConvertFromMinutes(float time, TimeMetric timeMetric, double hoursPerDay)
        {
            double dTime = time;

            double hours = dTime / MinutesPerHour;
            double days = hours / hoursPerDay;
            double weeks = hours / DaysPerWeek;
            double months = days / DaysPerMonth;

            switch (timeMetric)
            {
                case TimeMetric.Hours:
                    dTime = hours;
                    break;
                case TimeMetric.Days:
                    dTime = days;
                    break;
                case TimeMetric.Weeks:
                    dTime = weeks;
                    break;
                case TimeMetric.Months:
                    dTime = months;
                    break;
            }

            time = float.Parse(dTime.ToString());
            return time;
        }

        public static float ConvertToMinutes(float time, TimeMetric timeMetric, double hoursPerDay)
        {
            double dTime = time;

            double hours = dTime * MinutesPerHour;
            double days = hours * hoursPerDay;
            double weeks = days * DaysPerWeek;
            double months = days * DaysPerMonth;

            switch (timeMetric)
            {
                case TimeMetric.Hours:
                    dTime = hours;
                    break;
                case TimeMetric.Days:
                    dTime = days;
                    break;
                case TimeMetric.Weeks:
                    dTime = weeks;
                    break;
                case TimeMetric.Months:
                    dTime = months;
                    break;
            }

            dTime = Math.Round(dTime, 0);

            time = float.Parse(dTime.ToString());
            return time;
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Common/Utility/FormattingHelpers.cs $
 * 
 * 13    1/27/10 4:29p Camerons
 * 
 * 12    1/06/10 12:39p Camerons
 * 
 * 11    12/07/09 3:16p Stefanf
 * 
 * 10    10/09/09 10:30a Stefanf
 * 
 * 9     30/09/09 9:12a Shafia
 * 
 * 8     17/08/09 10:38a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/